package me.app.login;

import me.app.global.MyApplication;
import me.where.home.R;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageButton;

import com.ab.activity.AbActivity;
import com.ab.util.AbStrUtil;

public class LoginActivity extends AbActivity {

	private EditText etUserName = null;
	private EditText etUserPwd = null;
	private MyApplication myApplication;
	private String mStrName = null;
	private String mStrPwd = null;
	private ImageButton mClearBtnN;
	private ImageButton mClearBtnP;
	private Button loginBtn = null;
	private Button registerBtn = null;
	private Button findPwdBtn = null;
	private CheckBox cbRemPwd = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setAbContentView(R.layout.activity_login);

		this.myApplication = (MyApplication) this.getApplication();

		this.mAbTitleBar.setTitleText(R.string.login);
		this.mAbTitleBar.setLogo(R.drawable.button_selector_back);
		this.mAbTitleBar.setTitleTextMargin(10, 0, 0, 0);
		this.mAbTitleBar.setTitleBarBackground(R.drawable.top_bg);
		this.mAbTitleBar.setLogoLine(R.drawable.line);

		this.etUserName = (EditText) findViewById(R.id.userName);
		this.etUserPwd = (EditText) findViewById(R.id.userPwd);
		this.cbRemPwd = (CheckBox) findViewById(R.id.login_check);
		this.mClearBtnN = (ImageButton) findViewById(R.id.clearName);
		this.mClearBtnP = (ImageButton) findViewById(R.id.clearPwd);
		this.loginBtn = (Button) findViewById(R.id.loginBtn);
		this.registerBtn = (Button) findViewById(R.id.registerBtn);
		this.findPwdBtn = (Button) findViewById(R.id.pwdBtn);

		this.loginBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mStrName = etUserName.getText().toString().trim();
				mStrPwd = etUserPwd.getText().toString().trim();
				if (AbStrUtil.isEmpty(mStrName)) {
					showToast(R.string.error_name);
					etUserName.setFocusable(true);
					etUserName.requestFocus();
					return;
				}
				if (!AbStrUtil.isNumberLetter(mStrName)) {
					showToast(R.string.error_name_expr);
					etUserName.setFocusable(true);
					etUserName.requestFocus();
					return;
				}
				if (AbStrUtil.strLength(mStrName) >= 10) {
					showToast(R.string.error_name_len2);
					etUserName.setFocusable(true);
					etUserName.requestFocus();
					return;
				}
				if (AbStrUtil.isEmpty(mStrPwd)) {
					showToast(R.string.error_pwd);
					etUserPwd.setFocusable(true);
					etUserPwd.requestFocus();
					return;
				}
				if (!AbStrUtil.isNumberLetter(mStrPwd)) {
					showToast(R.string.error_pwd_expr);
					etUserPwd.setFocusable(true);
					etUserPwd.requestFocus();
					return;
				}
				if (AbStrUtil.strLength(mStrPwd) > 10) {
					showToast(R.string.error_pwd_len2);
					etUserPwd.setFocusable(true);
					etUserPwd.requestFocus();
					return;
				}
			}

		});
		this.findPwdBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(LoginActivity.this,
						FindPwdActivity.class);
				startActivity(intent);
			}

		});

		this.registerBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(LoginActivity.this,
						RegisterActivity.class);
				startActivity(intent);
			}

		});
		this.etUserName.addTextChangedListener(new TextWatcher() {

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub
				String str = etUserName.getText().toString().trim();
				if (str.length() > 0) {
					mClearBtnN.setVisibility(View.VISIBLE);
					mClearBtnN.postDelayed(new Runnable() {

						@Override
						public void run() {
							// TODO Auto-generated method stub
							mClearBtnN.setVisibility(View.INVISIBLE);
						}

					}, 3000);
				}
			}

		});
		this.etUserPwd.addTextChangedListener(new TextWatcher() {

			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub
				String str = etUserPwd.getText().toString().trim();
				if (str.length() > 0) {
					etUserPwd.setVisibility(View.VISIBLE);
					mClearBtnP.postDelayed(new Runnable() {

						@Override
						public void run() {
							// TODO Auto-generated method stub
							mClearBtnP.setVisibility(View.INVISIBLE);
						}

					}, 3000);
				}
			}

		});
		this.mClearBtnN.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				etUserName.setText("");
			}

		});
		this.mClearBtnP.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				etUserPwd.setText("");
			}

		});
		this.cbRemPwd.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton btnView,
					boolean isChecked) {
				// TODO Auto-generated method stub

			}

		});
		this.mAbTitleBar.getLogoView().setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						finish();
					}

				});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.login, menu);
		return true;
	}

}
